//
//  CalendarJobTableViewCell.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/22.
//

import UIKit
import SnapKit
class CalendarJobTableViewCell: TableViewCell {
    let nameLabel = ContentLabel(R.font.interBold(size: 18),hexStr: "#101E30FF")
    let addressLabel = ContentLabel(hexStr: "#292D32B2")
    let jobNoView: JobTypeAndJobNoView = JobTypeAndJobNoView()
    var jobStatusView = JobsListStatusView()
    lazy var pickUpRequiredBtn = LayoutButton(.left,at: 10)
    //天气信息
    let tempretureLabel = ContentLabel(R.font.interMedium(size: 16))
    let weatherImageView = UIImageView()
    override func setUpviews() {
        let backView = CustomBackView()
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        
        //weatherImageView
        backView.addSubview(weatherImageView)
        weatherImageView.snp.makeConstraints { make in
            make.left.top.equalTo(20)
            make.size.equalTo(24)
        }
        //tempretureLabel
        backView.addSubview(tempretureLabel)
        tempretureLabel.snp.makeConstraints { make in
            make.left.equalTo(weatherImageView.snp.right).offset(5)
            make.centerY.equalTo(weatherImageView)
            make.size.greaterThanOrEqualTo(0)
        }
        //name
        nameLabel.numberOfLines = 0
        backView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(20)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        let moreImageView = ViewFactory.shared.generateMoreImageView()
        backView.addSubview(moreImageView)
        moreImageView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(5)
            make.right.equalTo(-16)
            make.centerY.equalTo(nameLabel)
            make.height.width.equalTo(24)
        }
        //gwt no
        backView.addSubview(jobNoView)
        jobNoView.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(15)
            make.left.equalTo(nameLabel)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(jobStatusView)
        jobStatusView.snp.makeConstraints { make in
            make.centerY.equalTo(jobNoView)
            make.right.equalTo(-16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //address
        addressLabel.numberOfLines = 0
        backView.addSubview(addressLabel)
        addressLabel.snp.makeConstraints { make in
            make.top.equalTo(jobNoView.snp.bottom).offset(14)
            make.left.equalTo(nameLabel)
            make.right.equalTo(moreImageView)
            make.height.greaterThanOrEqualTo(0)
        }
        //pickUpRequiredBtn
        pickUpRequiredBtn.backgroundColor = UIColor(hexStr: Constant.SystemBackColor)
        pickUpRequiredBtn.layer.cornerRadius = 6
        pickUpRequiredBtn.clipsToBounds = true
        pickUpRequiredBtn.setBtnInfo(R.string.localizable.pickUpRequired(),hexStr: "#292D32", textFont: R.font.interMedium(size: 14), imageName: "calender_job_list_pickuprequired")
        backView.addSubview(pickUpRequiredBtn)
        pickUpRequiredBtn.snp.makeConstraints { make in
            make.top.equalTo(addressLabel.snp.bottom).offset(12)
            make.left.equalTo(nameLabel)
            make.right.equalTo(-16)
            make.height.equalTo(40)
            make.bottom.equalTo(-20)
        }
    }
    func setData(_ model: InstallationListModel){
        self.nameLabel.text = model.full_name
        self.jobNoView.setData(type: model.solar_water_heater ? .swhJob : .pvdJob, jobNo: model.gwt_id)
        self.addressLabel.text = model.full_install_address
        //pick up required
        if model.pick_up_required {
            self.pickUpRequiredBtn.isHidden = false
            addressLabel.snp.remakeConstraints { make in
                make.top.equalTo(jobNoView.snp.bottom).offset(14)
                make.left.equalTo(nameLabel)
                make.right.equalTo(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            pickUpRequiredBtn.snp.remakeConstraints { make in
                make.top.equalTo(addressLabel.snp.bottom).offset(12)
                make.left.equalTo(nameLabel)
                make.right.equalTo(-16)
                make.height.equalTo(40)
                make.bottom.equalTo(-20)
            }
        }else{
            self.pickUpRequiredBtn.isHidden = true
            addressLabel.snp.remakeConstraints { make in
                make.top.equalTo(jobNoView.snp.bottom).offset(14)
                make.left.equalTo(nameLabel)
                make.right.equalTo(-16)
                make.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(-20)
            }
            pickUpRequiredBtn.snp.remakeConstraints { make in
                make.bottom.equalTo(addressLabel.snp.bottom)
                make.left.equalTo(nameLabel)
                make.right.equalTo(-16)
            }
        }
        self.jobStatusView.setData(model)
        let formatInstallDate = DateUtil().convertDateStrToDateStr(dateStr: model.install_date, oldFormat: "MMM dd yyyy", newFormat: "yyyy-MM-dd")//job的安装日期
        //有经纬度，且安装日期有天气信息
        if self.checkIfHavaWeartherInfo(formatInstallDate) && model.location.latitude != "" && model.location.longitude != ""{//
            self.nameLabel.snp.remakeConstraints { make in
                make.top.equalTo(self.weatherImageView.snp.bottom).offset(12)
                make.left.equalTo(20)
                make.height.width.greaterThanOrEqualTo(0)
            }
            //加载天气信息
            self.loadWeatherInfo(latitude: model.location.latitude, longitude: model.location.longitude) { [weak self] (list) in
                guard let strongSelf = self else {return}
                if let weatherModel = list.first(where: {$0.date == formatInstallDate}){
                    strongSelf.tempretureLabel.text = weatherModel.mintemp_c + "°C - " + weatherModel.maxtemp_c + "°C"
                    strongSelf.weatherImageView.image = UIImage(named: weatherModel.condition_icon_seq)
                }
            }
        }else{//其他日期
            self.tempretureLabel.text = ""
            self.weatherImageView.image = nil
            self.nameLabel.snp.remakeConstraints { make in
                make.top.equalTo(20)
                make.left.equalTo(20)
                make.size.greaterThanOrEqualTo(0)
            }
        }
    }
    //判断install date是否油田气信息
    private func checkIfHavaWeartherInfo(_ installDate: String)->Bool{
        let date1 = DateUtil().getDatetimeStr(Date(),format: "yyyy-MM-dd",timeZone: TimeZone.current)//今天
        let date2 = DateUtil().getDatetimeStr(DateUtil().getServeralYearOfToday(date: Date(),day: 1),format: "yyyy-MM-dd",timeZone: TimeZone.current)//明天
        let date3 = DateUtil().getDatetimeStr(DateUtil().getServeralYearOfToday(date: Date(),day: 2),format: "yyyy-MM-dd",timeZone: TimeZone.current)//后天
        let weartherDays = [date1,date2,date3]
        if weartherDays.contains(installDate) {
            return true
        }
        return false
    }
    //MARK: - 加载天气信息
    private func loadWeatherInfo(latitude: String, longitude: String,closure: (([DayWeatherModel])->Void)? = nil){
        let service = DictionaryService()
        service.setBlockAfterNetwork { (result, otherInfo) in
            if let list = result as? [DayWeatherModel]{
                closure?(list)
            }
        } errorBlock: { code, error in
        }
        service.getWeather(latitude: latitude, longitude: longitude)
    }
}
class JobTypeAndJobNoView: View{
    let jobTypeImageView = UIImageView()
    var jobNoLabel: ContentLabel = {
        let view = ContentLabel()
        view.backgroundColor = .white
        view.setFont(R.font.interMedium(size: 12))
        view.textColor = UIColor(hexStr: Constant.ThemeColorHexStr)
        
        return view
    }()
    override func setUpView() {
        self.backgroundColor = .white
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor(hexStr: Constant.ThemeColorHexStr).cgColor
        self.layer.cornerRadius = 12
        //jobType imageview
        self.addSubview(jobTypeImageView)
        jobTypeImageView.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.top.equalTo(4)
            make.bottom.equalTo(-4)
            make.width.height.equalTo(15)
        }
        //job no
        self.addSubview(jobNoLabel)
        jobNoLabel.snp.makeConstraints { make in
            make.left.equalTo(jobTypeImageView.snp.right).offset(4)
            make.right.equalTo(-8)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
    }
    func setData(type: JobType, jobNo: String){
        switch type {
        case .pvdJob:
            self.jobTypeImageView.image = R.image.calendar_job_list_cell_pvd_type()
        case .swhJob:
            self.jobTypeImageView.image = R.image.calendar_job_list_cell_swh_type()
        default:
            break
        }
        self.jobNoLabel.text = jobNo
    }
}
